package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.domain.entity.LoginUser;
import com.example.domain.entity.User;
import com.example.mapper.UserMapper;
import com.mysql.cj.x.protobuf.MysqlxExpr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //根据用户名查询用户信息，判断是否查到
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getUserName,s);
        User user = userMapper.selectOne(qw);
        //没查到，抛出异常
        if(Objects.isNull(user)){
            throw new RuntimeException("用户名不存在");
        }
        //查到了，返回UserDetail对象，密码被自动校验
        /** 还需要封装权限信息*/
        return new LoginUser(user);
    }
}
